#!/bin/bash
#Program
#	You input your demobilization date, I calculate how many days befor you demobilize
#History
#	2013/7/1	Leaguenew	First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

echo "This program will calculate "
echo "how many days before your demobilization date:"
read -p "Please input your demobilization date (YYYYMMDD , ex > 20130701 ) : "  date2




date_d=$(echo $date2 | grep '[0-9]\{8\}'  )	#check whether date2 contain 8 charactors
#echo "date_d = $date_d and date2 = $date2"


if [ "$date2" == "" ] ; then
	echo "You input the wrong date format..."
	exit 1;
fi

declare -i date_dem=`date --date="$date2" +%s`
echo "date_dem = $date_dem"

declare -i date_now=`date +%s`
echo "date_now = $date_now"

declare -i date_total_s=$date_dem-$date_now #"="号之间不能有空格
declare -i date_d=$date_total_s/60/60/24 

if [ "$date_total_s" -lt "0" ] ; then
	echo "You had been demobilization before : "$((-1*$date_d)) " ago   " #必须有两个括号不知道为什么
else
	declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60)) #不知道为什么要加那么多括号？
	echo "You will demobilize after $date_d days and $date_h hours..."
fi





